package com.ekuaitu.kuaitu.map;

import android.content.Context;
import android.widget.TextView;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;

/**
 * Created by Michael Pi on 2016/11/17.
 */

public class GeocodeSearchUtils implements GeocodeSearch.OnGeocodeSearchListener {
    private static GeocodeSearchUtils instance;
    private GeocodeSearch geocoderSearch;
    private LatLonPoint latLngPoint;
    private TextView view;

    public static GeocodeSearchUtils getInstance() {
        if (instance == null) {
            instance = new GeocodeSearchUtils();
        }
        return instance;
    }

    private GeocodeSearchUtils() {
    }

    public void startGeocodeSearch(Context context, TextView textView, double latitude, double longitude) {
        geocoderSearch = new GeocodeSearch(context);
        this.view = textView;
        geocoderSearch.setOnGeocodeSearchListener(this);
        latLngPoint = new LatLonPoint(latitude, longitude);
        RegeocodeQuery query = new RegeocodeQuery(latLngPoint, 20, GeocodeSearch.AMAP);
        geocoderSearch.getFromLocationAsyn(query);
    }


    @Override
    public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
        if (rCode == 1000) {
            if (result != null) {
                if (result.getRegeocodeAddress() != null) {
                    String district = "";
                    String township = "";
                    String neighborhood = "";
                    String building = "";
                    String aoisName = "";
                    String poisName = "";
                    if (result.getRegeocodeAddress().getDistrict() != null) {
                        district = result.getRegeocodeAddress().getDistrict();
                    }
                    if (result.getRegeocodeAddress().getTownship() != null) {
                        township = result.getRegeocodeAddress().getTownship();
                    }

                    if (result.getRegeocodeAddress().getNeighborhood() != null) {
                        neighborhood = result.getRegeocodeAddress().getNeighborhood();
                    }
                    if (result.getRegeocodeAddress().getBuilding() != null) {
                        building = result.getRegeocodeAddress().getBuilding();
                    }
                    if (result.getRegeocodeAddress().getAois() != null
                            && result.getRegeocodeAddress().getAois().size() != 0
                            && result.getRegeocodeAddress().getAois().get(0) != null
                            && result.getRegeocodeAddress().getAois().get(0).getAoiName() != null) {
                        aoisName = result.getRegeocodeAddress().getAois().get(0).getAoiName();
                    }

                    if (!building.equals("")) {
                        view.setText(district + township + building);
                    } else if (!neighborhood.equals("")) {
                        view.setText(district + township + neighborhood);
                    } else if (!aoisName.equals("")) {
                        view.setText(district + township + aoisName);
                    } else if (!poisName.equals("")) {
                        view.setText(district + township + poisName);
                    } else if (result.getRegeocodeAddress().getRoads() != null && result.getRegeocodeAddress().getRoads().size() != 0
                            && result.getRegeocodeAddress().getRoads().get(0).getName() != null) {
                        view.setText(district + township + result.getRegeocodeAddress().getRoads().get(0).getName());
                    } else {
                        view.setText(district + township);
                    }
                } else {
                    view.setText("地址解析失败...");
                }
            } else {
                view.setText("地址解析失败...");
            }

        } else {
            view.setText("地址解析失败...");
        }
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

    }
}
